#!/bin/sh
#-
# Copyright (c) 2002-2006 FreeBSD GNOME Team <freebsd-gnome@FreeBSD.org>
# Copyright (c) 2009 KDE FreeBSD Team <kde@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Based on marcusmerge, adapted for area51
#       by Martin Wilke <miwi@FreeBSD.org>
#
#

# User editable section.
# The following variables can be defined on the command line,
# or hardcoded below in the script.

# Path to the KDE ports without the trailing /. 
[ -n "$KDEDIR" ] || KDEDIR="`cd ../../KDE && pwd`"
# Path to the official ports collection.
[ -n "$PORTSDIR" ] || PORTSDIR="/usr/ports" 

# End of user editable section.
# Do NOT change anything below this line!

# Check input for trailing slashes, resolve symbolic links.
# Return real path.
KDEDIR=`/bin/realpath ${KDEDIR}`
PORTSDIR=`/bin/realpath ${PORTSDIR}`

# Merge UPDATING
if [ -f ${KDEDIR}/../UPDATING-area51 ]; then
    cp ${KDEDIR}/../UPDATING-area51 ${PORTSDIR}/
fi

# Merge MK files
echo "===> Merging files from area51 to the ports directory"
echo "${KDEDIR} --> ${PORTSDIR}"

if [ -d ${KDEDIR}/Mk ]; then
    for mk in `ls -1 ${KDEDIR}/Mk/*.mk`; do
	cp ${mk} ${PORTSDIR}/Mk
    done
fi

#Merge Category and Ports
for categorypath in `find ${KDEDIR} -type d -depth 1`; do
 	category=$(basename "$categorypath")

  if [ ${category} = ".svn" ]; then
	continue
   fi
   for port in `ls -1 ${KDEDIR}/${category}`; do
	if [ ${port} = ".svn" ]; then
	    continue
	fi
	if [ ! -f ${KDEDIR}/${category}/${port}/Makefile ]; then
	    continue
	fi
	if [ ! -d ${PORTSDIR}/${category} ]; then

	    mkdir -p ${PORTSDIR}/${category}
	fi
	if [ -d ${PORTSDIR}/${category}/${port} ]; then

	    rm -rf ${PORTSDIR}/${category}/${port}/work
	    find ${PORTSDIR}/${category}/${port} \! -path "*/.svn/*" -type f | \
	    	xargs rm -f
	fi

	cd ${KDEDIR}/${category}
	tar --exclude "*/.svn*" -cf - ${port} | \
		tar -xf - -C ${PORTSDIR}/${category}
    done
done

echo ""

echo "You can now run \"portupgrade -a\" or \"portmaster -a\" to upgrade your ports tree. Please read ${PORTSDIR}/UPDATING-area51 before you run portupgrade or portmaster. We suggest that you backup your ~/.kde4 configuration dir and start with a clean config." | /usr/bin/fmt 75 79
